const express=require('express')
const router= express.Router();
const db=require('../../utils/db')

// // 查询收银流水
// router.get('/getcashierwater',function(req,res){
//     let mysql=`SELECT cash_rank,order_num,table_position,table_num,pay_way,cash_money,dis_money,user_name,order_time,customer_name,order_payfor_num FROM cash_flowing 
//                JOIN order_information ON order_information.order_id=cash_flowing.cash_num
//                JOIN table_area ON cash_flowing.cash_table_area=table_area.table_id
//                JOIN discounts_management ON cash_flowing.cash_dis_limit=discounts_management.dis_id
//                JOIN pay_method ON order_information.order_pay_way=pay_method.pay_id
//                JOIN customer ON order_information.order_username=customer.customer_id
//                JOIN USER ON order_information.order_user_id=user.user_id GROUP BY order_num`
//     db(mysql,null,function(err,data){
//         if(err){
//             return res.json({
//                 code: 500,
//                 msg: '查询收银流水失败，请稍后重试'
//             })
//         }
//         res.json({
//             code: 200,
//             msg: '查询收银流水成功',
//             data: data
//         })
//     })
// })

// 查询收银流水
router.get('/getcashierwater',function(req,res){
    let mysql=`SELECT cash_rank,order_num,table_position,table_num,pay_way,cash_money,dis_money,user_name,order_time,customer_name,order_payfor_num FROM cash_flowing 
               JOIN order_information ON order_information.order_id=cash_flowing.cash_num
               JOIN table_area ON cash_flowing.cash_table_area=table_area.table_id
               JOIN discounts_management ON cash_flowing.cash_dis_limit=discounts_management.dis_id
               JOIN pay_method ON order_information.order_pay_way=pay_method.pay_id
               JOIN customer ON order_information.order_username=customer.customer_id
               JOIN USER ON order_information.order_user_id=user.user_id
               WHERE 1=1 `
    if(req.query.orderId){
        mysql+=`AND order_num LIKE '%${req.query.orderId}%'`
    }
    if(req.query.time[0]){
        mysql+=`AND order_time BETWEEN '${req.query.time[0]}' AND '${req.query.time[1]}'`
    }
    if(req.query.payWay){
        mysql+=`AND pay_way='${req.query.payWay}'`
    }
    if(req.query.tableArea){
        mysql+=`AND table_position='${req.query.tableArea}'`
    }
    mysql+=` GROUP BY order_num`
    db(mysql,null,function(err,data){
        if(err){
            return res.json({
                code: 500,
                msg: '模糊查询订单失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '模糊查询订单成功',
            data: data
        })
    })
})

router.get('/getpayway',function(req,res){
    let mysql=`SELECT * FROM pay_method`
    db(mysql,null,function(err,data){
        if(err){
            return res.json({
                code: 500,
                msg: '查询支付方式失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询支付方式成功',
            data: data
        })
    })
})



module.exports = router